<?php

namespace App\Http\Service;

use App\Models\StoreOrder;
use App\Models\StoreOrderCartInfo;
use App\Models\StoreProduct;
use Illuminate\Support\Facades\DB;

class OrderService
{

    /**
     * 核销码
     * @param $id
     * @return mixed
     * @throws \Exception
     */
    public static function verify_code($id){
        $order=StoreOrder::where(['uid'=>$id,'status'=>3])->select('uid','store_id','clerk_id','order_id')->get();

        if (!$order){
            throw new \Exception('暂无数据');
        }
        foreach ($order as $value){
            $verify_code =$value->store_id.$value->clerk_id.$value->uid.time().rand(1000,9999);
            $res=StoreOrder::where('order_id',$value->order_id)->update(['verify_code'=>$verify_code]);
        }
        if(!$res){
            throw  new  \Exception('错误');
        }
        return $res;
    }

    /**
     * @param $data //查询多个条件
     * @return array //返回数据
     * 订单数据后台查询
     */
    public static function OrderList($data)
    {

        $where = [];

        if (empty($data['list'])){
            return ['code'=>401,'msg'=>'页码参数错误'];
        }

        if (empty($data['page'])){
            return ['code'=>401,'msg'=>'页码参数错误'];
        }

        if (!empty($data['order_id'])){
            $where = ['order_id','like',$data['order_id']];
        }

        if (!empty($data['refund_status'])){
            $where = ['refund_status','like',$data['refund_status']];
        }

        if (!empty($data['real_name'])){
            $where = ['real_name','like',$data['real_name']];
        }

        if (!empty($data['status'])){
            $where = ['status','like',$data['status']];
        }

        if(!empty($data['uid'])){
            $where = ['uid','like',$data['uid']];
        }

        if (!empty($data['shipping_type'])){
            $where = ['shipping_type','like',$data['shipping_type']];
        }

        $page = $data['page'];
        $list = $data['list'];
        $pageName = $data['pageName'];

        $list_data = StoreOrder::with('OrderCart')->where($where)
                     ->orderBy('add_time','desc')
                     ->paginate(5);

        if (empty($list_data)){
            return ['code'=>401,'msg'=>'数据为空','data'=>$list_data];
        }
            return ['code'=>200,'msg'=>'返回数据','data'=>$list_data];

    }

    /**
     * @param $id
     * @return array
     * 对订单进行后台删除
     */
    public static function OrderDelete($id)
    {

        $data_true = StoreOrder::where('id',$id)->update(['is_system_del'=>1]);


        if (!$data_true)
        {
           return ['code'=>501,'msg'=>'删除失败'];
        }
           return ['code'=>200,'msg'=>'删除成功'];
    }

   //对订单进行添加
    public static function OrderAdd($data)
    {
        DB::beginTransaction();
        try {

            OrderNumberGeneration::machineId($data['uid']);
            $order_id = OrderNumberGeneration::createOnlyId();

            $order_data = [
                'order_id'=>$order_id,
                'uid'=>$data['uid'],
                'real_name'=>$data['real_name'],
                'user_phone'=>$data['user_phone'],
                'user_address'=>$data['user_address'],
                'cart_id'=>$data['cart_id'],
                'freight_price'=>$data['freight_price'],
                'total_num'=>$data['total_num'],
                'total_price'=>$data['total_price'],
                'total_postage'=>$data['total_postage'],
                'pay_price'=>$data['pay_price'],
                'pay_postage'=>$data['pay_postage'],
                'deduction_price'=>$data['deduction_price'],
                'coupon_id'=>$data['coupon_id'],
                'coupon_price'=>$data['coupon_price'],
                'add_time'=>time(),
            ];

            $data_true = StoreOrder::create($order_data);

            if (empty($data_true->id))
            {
                throw new \Exception(['code'=>500,'msg'=>'订单生成失败']);
            }

            $oid = $data_true->id;

            foreach ($data['goods'] as $k=>$v){

                $goods_data = StoreProduct::OrderSelect($v);
                $goods_data = $goods_data;
                $goods_data = json_encode($goods_data);
                OrderNumberGeneration::machineId($v);
                $carts_id = OrderNumberGeneration::createOnlyId();
                $cart_data = [
                    'oid'=>$oid,
                    'cart_id'=>$data['cart_id'],
                    'product_id'=>$v,
                    'cart_info'=>$goods_data,
                    'unique'=>$carts_id
                ];
                $cart_true = StoreOrderCartInfo::create($cart_data);
                if (empty($cart_true->unique)){
                    throw new \Exception(['code'=>500,'msg'=>'订单购物车生成失败']);
                }
            }
            DB::commit();
            return ['code'=>200,'msg'=>'订单添加成功','data'=>$oid];
        }catch (\Exception $exception){
            DB::rollBack();
            return $exception;
        }

    }




    //对订单进行修改
    public static function OrderUpdata($id,$data)
    {
        $data_true = StoreOrder::UpData($id,$data);
        if (!$data_true)
        {
            return ['code'=>501,'msg'=>'订单修改失败'];
        }
            return ['code'=>200,'msg'=>'订单修改成功'];
    }








}
